<template>
  <el-dialog
    class="sendNotice"
    title="发送受理通知书"
    :visible.sync="isShow"
    center
    :close="shoutongClose"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="120px"
      label-position="top"
    >
      <!-- <el-form-item label="申报名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入案件申报名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="申请号：" prop="application_number">
        <el-input
          v-model="form.application_number"
          placeholder="请输入案件申请号"
        ></el-input>
      </el-form-item>
      <el-form-item label="提交日期：" prop="submit_at">
        <el-date-picker
          v-model="form.submit_at"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择提交日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item label="受理通知书" prop="slfile">
        <manualUpload
          :typeLimit="['.pdf', '.zip', '.rar', '.7z']"
          :numLimit="1"
          @change="upChange"
          ref="manualUpload"
        >
        </manualUpload>
        <div style="color: #999;" class="el-upload__tip">
          文件格式为：日期_订单编号_专利名称_通知书_申请号_代理机构名称
        </div>
        <div style="color: #999;" class="el-upload__tip">
          例：yyyy-mm-dd_#############_###_专利申请受理通知书_#############_某机构
        </div>
      </el-form-item>
    </el-form>

    <!-- <div class="tipWarp">
      <div class="tiptip">
        一个文件直接上传,如果有多个文件,请将文件夹压缩后上传
      </div>
      <div class="tiptip">文件格式为：日期-订单编号-专利名称-通知书-申请号</div>
      <div class="tiptip">
        例：yyyy-mm-dd_#############_###_专利申请受理通知书_#############
      </div>
    </div> -->
    <!-- <div class="sendNotice_btn">
      
    </div> -->
    <span slot="footer">
      <el-button size="mini" @click="cancel">取 消</el-button>
      <el-button type="success" size="mini" :loading="isSendBtnLoading" @click="sendBtn"
        >确认上传</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import manualUpload from "~/components/manualUpload";
import axios from "axios";
export default {
  components: {
    manualUpload,
  },
  data() {
    return {
      isShow: false,
      orderdata: null,
      form: {
        name: "",
        application_number: "",
        file: null,
        submit_at: "",
      },
      rules: {
        application_number: [
          {
            required: true,
            message: "请输入案件申请号",
            trigger: "blur",
          },
          { min: 13, max: 13, message: "申请号长度在13字符", trigger: "blur" },
        ],
        name: [
          {
            required: true,
            message: "请输入案件申报名称",
            trigger: "blur",
          },
        ],
      },
      isSendBtnLoading: false,
      baseUrl: global.baseurl,
    };
  },
  methods: {
    show(row) {
      this.isShow = true;
      this.orderdata = row;
    },
    cancel() {
      this.isShow = false;
      this.$refs["form"].resetFields();
      this.$refs["manualUpload"].clearFiles();
    },
    upChange(data) {
      console.log(data)
      if (data.length) {
        let canUpload = true; //是否可以上传
        let fileName,
          fileNameArr,
          arrLen,
          pos,
          fileDate,
          lastName,
          finalName,
          application,
          agencyName,
          orderNum = "";
        let dateReg = /^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$/;
        // //验证文件类型
        fileName = data[0].name;
        pos = fileName.lastIndexOf(".");
        // 获取文件名中的日期
        fileDate = `${fileName.split("_")[0]}`;
        // 去除时间开始抽取订单编号
        orderNum = fileName.substr(11, this.orderdata.order_number.length);
        // 申报名称
        finalName = fileName
          .substring(11 + this.orderdata.order_number.length + 1)
          .split("_")[0];
        // 后缀名
        lastName = fileName.substring(pos, fileName.length);
        // 申请号
        arrLen = fileName.split("_");
        application = arrLen[arrLen.length - 2].split(".")[0];
        agencyName = arrLen[arrLen.length - 1].split(".")[0];

        if (!dateReg.test(fileDate)) {
          this.$message.error("日期格式错误，正确的格式为：yyyy-mm-dd");
          canUpload = false;
        } else if (orderNum != this.orderdata.order_number) {
          this.$message.error("订单编号不正确");
          canUpload = false;
        } 
        // else if (finalName != this.orderdata.name) {
        //   this.$message.error("申报名称不正确");
        //   canUpload = false;
        // }
        else if (application.length != 13) {
          this.$message.error("申请号格式不正确");
          canUpload = false;
        } else if (agencyName != this.$store.state.user.agency.agency_name) {
          this.$message.error("机构名称不正确");
          canUpload = false;
        } else if (
          lastName.toLowerCase() !== ".pdf" &&
          lastName.toLowerCase() !== ".rar" &&
          lastName.toLowerCase() !== ".zip"
        ) {
          this.$message.error("文件必须为 pdf 格式或 rar/zip格式");
          canUpload = false;
        }
        if (canUpload) {
          this.form.file = data[0].raw;
        } else {
          this.form.file = null;
          this.$refs["manualUpload"].clearFiles();
        }
      } else {
        this.form.file = null;
        this.$refs["manualUpload"].clearFiles();
      }
    },
    shoutongClose(data) {
      this.isShow = false;
      data = [];
    },
    sendBtn() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (!this.form.file) {
            this.$message("请上传受理通知书！");
            return;
          }
          this.sendSubmit();
        }
      });
    },
    sendSubmit() {
      let finals = this.orderdata.id;

      let params = new FormData();
      // for (let key in this.form) {
      //   params.append(key, this.form[key]);
      // }
      params.append("file", this.form.file);
      this.isSendBtnLoading = true;
      axios({
        method: "post",
        url: this.baseUrl + `/api/finals/${finals}/agency/acceptance/file`,
        data: params,
        headers: {
          "Content-Type": "multipart/form-data",
          Authorization: "Bearer " + $cookies.get("token"),
        },
      }).then((res) => {
        if (res.data.code == 200) {
          this.isSendBtnLoading = false;
          this.$alert("已发送受理通知书！", "提示", {
            confirmButtonText: "确定",
            callback: (action) => {},
          });
          this.$emit("refresh");
          this.cancel();
        } else {
          this.isSendBtnLoading = false;
          this.$alert(res.data.message, "提示", {
            confirmButtonText: "确定",
            callback: (action) => {},
          });
        }
      });
    },
  },
};
</script>

<style lang="less">
.sendNotice {
  .el-dialog {
    width: 500px;
  }
  .titleST {
    margin-bottom: 20px;
  }
  .el-date-editor {
    width: 100%;
  }
  .tipWarp {
    margin-top: 20px;
    .tiptip {
      font-size: 12px;
    }
  }
  .sendNotice_btn {
    text-align: center;
  }
}
</style>
